<template>
  <div>
    <yfuPTable ref="table" :param="tableParam" v-show="isShowTable"></yfuPTable>
    <yfuCommonTableDialog ref="dialog" :param="dialogParam" v-on:fmModuleToRefreshTable="fmModuleToRefreshTable"></yfuCommonTableDialog>
    <companyManagerDesc ref="richTextDesc" v-if="isShowRichTextDesc" :param="currentRow" v-on:returnPre="returnPre" v-on:returnPreNoRefresh="returnPreNoRefresh"></companyManagerDesc>
    <companyManagerDesc ref="richTextService" v-if="isShowRichTextService" :param="currentRow" v-on:returnPre="returnPre" v-on:returnPreNoRefresh="returnPreNoRefresh"></companyManagerDesc>
  </div>
</template>
<style>
</style>
<script>
  import yfuPTable from '@/components/common/yfuPTable.vue'
  import { yfuSuccessToast, yfuErrorToast } from '@/utils/yfuMessage.js'
  import { configList, saveConfig, deleteConfig, updateConfig, selectOneConfig, getCompanyIntroByCompanyId, getCompanyServiceByCompanyId } from '../api/company.js'
  import getPermissionByMenuId from '@/utils/MenuPermissionUtils.js'
  import yfuCommonTableDialog from '@/components/common/yfuCommonTableDialog.vue'
  import companyManagerDesc from '@/view/website/company/companyManagerDesc.vue'
  import companyManagerService from '@/view/website/company/companyManagerService.vue'
  import { yfuPost } from '@/utils/request.js'
  export default {
    components: {
      'yfuPTable': yfuPTable,
      'companyManagerDesc': companyManagerDesc,
      'companyManagerService': companyManagerService,
      'yfuCommonTableDialog': yfuCommonTableDialog
    },
    // 进入时初始化TABLE
    created () {
      let cThis = this
      getPermissionByMenuId(this.sessiongetJSON('user').id, this.menuId, function (permissionString) {
        cThis.permissions = permissionString
        cThis.tableParam.permissions = permissionString
        console.log(permissionString)
      })
    },
    methods: {
      createModuleDialog () {
        this.dialogStatus = 'create'
        this.$refs.dialog.createModuleDialog()
      },
      modifyModuleDialog (row) {
        this.dialogStatus = 'update'
        let cThis = this
        let param = {}
        param.id = row.id
        yfuPost(process.env.BASE_API, selectOneConfig.url, param, function (data) {
          cThis.$refs.dialog.modifyModuleDialog(data)
        })
      },
      fmModuleToRefreshTable () {
        this.$refs.table.refreshTable()
      },
      openRichTextDialog (row) {
        let cThis = this
        yfuPost(process.env.BASE_API, getCompanyIntroByCompanyId.url, {gwCompanyId: row.id}, function (data) {
          cThis.isShowRichTextService = true
          cThis.isShowTable = false
          cThis.currentRow = data
          cThis.currentRow.projectDetail = data.gwCompanyIntro
        })
      },
      openRichTextServiceDialog (row) {
        let cThis = this
        yfuPost(process.env.BASE_API, getCompanyServiceByCompanyId.url, {gwCompanyId: row.id}, function (data) {
          cThis.isShowRichTextDesc = true
          cThis.isShowTable = false
          cThis.currentRow = data
          cThis.currentRow.projectDetail = data.gwCompanyService
        })
      },
      returnPre () {
        this.isShowTable = true
        this.isShowRichTextDesc = false
        this.isShowRichTextService = false
        this.fmModuleToRefreshTable()
      },
      returnPreNoRefresh () {
        this.isShowTable = true
        this.isShowRichTextDesc = false
        this.isShowRichTextService = false
      }
    },
    data () {
      let cThis = this
      return {
        menuId: this.$route.query.id,
        isShowRichTextDesc: false,
        isShowTable: true,
        isShowRichTextService: false,
        tableParam: {
          permissions: '',
          showBox: true,
          baseURL: process.env.BASE_API,
          url: configList.url,
          urlParam: {
          },
          showToolButton: true,
          toolButtonSpan: 10,
          toolButtons: [
            {
              id: 1,
              title: '新建',
              type: 'primary',
              icon: 'el-icon-plus',
              visible: true,
              permission: '/tkCompanyAdd',
              operateFunction: function () {
                cThis.createModuleDialog()
              }
            },
            {
              id: 2,
              title: '删除',
              type: 'danger',
              icon: 'el-icon-delete',
              visible: true,
              permission: '/tkCompanyDelete',
              operateFunction: function () {
                // 获取选中行的数据
                if (cThis.$refs.table.getCheckedRows().length === 0) {
                  yfuErrorToast('请至少选中一行')
                } else {
                  cThis.$confirm('是否确认删除模块？', '删除模块', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                  }).then(() => {
                    let ids = []
                    cThis.$refs.table.getCheckedRows().forEach(element => {
                      console.log(element)
                      ids.push(element.id)
                    })

                    let params = {}
                    params.ids = ids.join()

                    yfuPost(process.env.BASE_API, deleteConfig.url, params,
                      function (data) {
                        console.log(data)
                        yfuSuccessToast('删除成功')
                        cThis.$refs.table.refreshTable()
                      },
                      function (error) {
                        console.log(error)
                        yfuErrorToast('删除失败')
                      }
                    )
                  }).catch(() => {
                    cThis.$message({
                      type: 'info',
                      message: '已取消删除'
                    })
                  })
                }
              }
            }
          ],
          toolSearchSpan: 12,
          toolSearchItems: [
          ],
          operateRow: {
            width: '200px'
          },
          showOperateRow: true,
          operateButtons: [
            {
              operateFunction: function (row) {
                console.log(row)
                cThis.modifyModuleDialog(row)
              },
              icon: 'edit',
              permission: '/tkCompanyEdit'
            },
            {
              operateFunction: function (row) {
                cThis.openRichTextDialog(row)
              },
              icon: 'details',
              permission: '/tkCompanyEditDesc'
            },
            {
              operateFunction: function (row) {
                cThis.openRichTextServiceDialog(row)
              },
              icon: 'detail',
              permission: '/tkCompanyEditService'
            }
          ],
          rows: [
            {
              prop: 'gwCompanyName',
              label: '公司名称'
            },
            {
              prop: 'gwCompanyQq',
              label: '公司QQ'
            },
            {
              prop: 'gwCompanyPhone',
              label: '公司电话'
            },
            {
              prop: 'gwCompanyEmail',
              label: '公司邮箱'
            },
            {
              prop: 'gwCompanyAddress',
              label: '公司地址'
            },
            {
              prop: 'gwCompanyBrief',
              label: '公司简介'
            }
          ]
        },
        dialogParam: {
          width: '35%',
          labelWidth: '120px',
          temp: {
            gwCompanyName: '',
            gwCompanyQq: '',
            gwCompanyPhone: '',
            gwCompanyEmail: '',
            gwCompanyAddress: '',
            gwCompanyBrief: ''
          },
          formItems: [
            {
              id: 1,
              prop: 'gwCompanyName',
              label: '公司名称：',
              inputType: 'text',
              rules: {
                required: true,
                trigger: 'blur',
                validator: function (rule, value, callback) {
                  if (value === '' || value === null || value === undefined) {
                    callback(new Error('请输入公司名称'))
                  } else {
                    callback()
                  }
                }
              }
            },
            {
              id: 2,
              prop: 'gwCompanyQq',
              label: '公司QQ：',
              inputType: 'text',
              rules: {
                required: true,
                trigger: 'blur',
                validator: function (rule, value, callback) {
                  if (value === '' || value === null || value === undefined) {
                    callback(new Error('请输入公司QQ'))
                  } else {
                    callback()
                  }
                }
              }
            },
            {
              id: 3,
              prop: 'gwCompanyPhone',
              label: '公司电话：',
              inputType: 'text',
              rules: {
                required: true,
                trigger: 'blur',
                validator: function (rule, value, callback) {
                  if (value === '' || value === null || value === undefined) {
                    callback(new Error('请输入公司电话'))
                  } else {
                    callback()
                  }
                }
              }
            },
            {
              id: 4,
              prop: 'gwCompanyEmail',
              label: '公司邮箱：',
              inputType: 'text',
              rules: {
                required: true,
                trigger: 'blur',
                validator: function (rule, value, callback) {
                  if (value === '' || value === null || value === undefined) {
                    callback(new Error('请输入公司邮箱'))
                  } else {
                    callback()
                  }
                }
              }
            },
            {
              id: 5,
              prop: 'gwCompanyAddress',
              label: '公司地址：',
              inputType: 'text',
              rules: {
                required: true,
                trigger: 'blur',
                validator: function (rule, value, callback) {
                  if (value === '' || value === null || value === undefined) {
                    callback(new Error('请输入公司地址'))
                  } else {
                    callback()
                  }
                }
              }
            },
            {
              id: 6,
              prop: 'gwCompanyBrief',
              label: '公司简介：',
              inputType: 'textarea',
              rules: {
                required: true,
                trigger: 'blur',
                validator: function (rule, value, callback) {
                  if (value === '' || value === null || value === undefined) {
                    callback(new Error('请输入公司简介'))
                  } else {
                    callback()
                  }
                }
              }
            }
          ],
          baseURL: process.env.BASE_API,
          createUrl: saveConfig.url,
          updateUrl: updateConfig.url,
          textMap: {
            update: '修改配置状态',
            create: '新建配置状态'
          },
          dialogStatus: 'update',
          dialogFormVisible: false
        }
      }
    }
  }
</script>
